package com.hospital.diagnose.controller;

import com.alibaba.fastjson.JSONObject;
import com.hospital.diagnose.dao.MedicalRecordMapper;
import com.hospital.diagnose.dao.PatientMapper;
import com.hospital.diagnose.entity.MedicalRecord;
import com.hospital.diagnose.service.PerscriptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @date 2019/1/14 11:23
 * @description TODO
 * @Version
 */
@Controller
@RequestMapping (value = "/perscription")
public class PerscriptionController {
    @Autowired
    PerscriptionService perscriptionService;
    @Autowired
    PatientMapper patientMapper;
    @Autowired
    MedicalRecordMapper medicalRecordMapper;
    @RequestMapping(value = "/all",produces = "application/json;charset=utf-8",method =RequestMethod.GET)
    @ResponseBody
    public String getAll(){
        return perscriptionService.getAll();
    }
    @RequestMapping(value = "/add",produces = "application/json;charset=utf-8",method =RequestMethod.POST)
    @ResponseBody
    public String add(@RequestBody String data)throws ParseException {
        String mdcRecAdvice=JSONObject.parseObject(data).getString("mdcRecAdvice");
        String mdcRecCheckpoint=JSONObject.parseObject(data).getString("mdcRecCheckpoint");
        String mdcRecCreateDate=JSONObject.parseObject(data).getString("mdcRecCreateDate");
        Date date=new SimpleDateFormat("yyyy-MM-dd").parse(mdcRecCreateDate);
        String mdcRecPatDescription=JSONObject.parseObject(data).getString("mdcRecPatDescription");
        String mdcRecPhysician=JSONObject.parseObject(data).getString("mdcRecPhysician");
        String mdcRecResult=JSONObject.parseObject(data).getString("mdcRecResult");
        String patName=JSONObject.parseObject(data).getString("patName");
        return perscriptionService.insertPerscription(patName,date,mdcRecPatDescription,mdcRecCheckpoint,mdcRecResult,mdcRecAdvice,mdcRecPhysician);
    }

   @RequestMapping(value = "/update",produces = "application/json;charset=utf-8",method =RequestMethod.POST)
    @ResponseBody
    public String update(@RequestBody String data){
        MedicalRecord medicalRecord=medicalRecordMapper.selectByPrimaryKey(Integer.parseInt(JSONObject.parseObject(data).getString("id")));
        medicalRecord.setMdcRecPatId(patientMapper.selectByPatName(JSONObject.parseObject(data).getString("patName")).getId());
        medicalRecord.setMdcRecResult(JSONObject.parseObject(data).getString("mdcRecResult"));
        medicalRecord.setMdcRecAdvice(JSONObject.parseObject(data).getString("mdcRecAdvice"));
        medicalRecord.setMdcRecCheckpoint(JSONObject.parseObject(data).getString("mdcRecCheckpoint"));
        //medicalRecord.setMdcRecResult(JSONObject.parseObject(data).getString("mdcRecCreateDate"));
        medicalRecord.setMdcRecPatDescription(JSONObject.parseObject(data).getString("mdcRecPatDescription"));
        medicalRecord.setMdcRecPhysician(JSONObject.parseObject(data).getString("mdcRecPhysician"));

        return perscriptionService.update(medicalRecord);
    }

    @RequestMapping(value = "/delete",produces = "application/json;charset=utf-8",method =RequestMethod.POST)
    @ResponseBody
    public String delete(@RequestBody String data){
        int id=Integer.parseInt(JSONObject.parseObject(data).getString("id"));
        return perscriptionService.deleteById(id);
    }




}
